package com.dai.zhenfujin.visitingcard;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioGroup;

import com.dai.zhenfujin.Constants.Constants;
import com.dai.zhenfujin.R;
import com.dai.zhenfujin.addresslist.fragment.AttentionPagerFragment;
import com.dai.zhenfujin.addresslist.fragment.FansPagerFragment;
import com.dai.zhenfujin.base.BaseFragment;
import com.dai.zhenfujin.my.activity.ReleaseActivity;
import com.dai.zhenfujin.utils.SPUtils;
import com.dai.zhenfujin.utils.UIUtils;
import com.dai.zhenfujin.visitingcard.activity.AlumActivity;

import java.util.ArrayList;
import java.util.List;

public class FuliFragment extends BaseFragment implements View.OnClickListener {
    private FrameLayout fl_alum;
    private ImageView imageView, iv_alum;
    private ListView listView;
    private ViewPager viewPager;
    private RadioGroup radioGroup;
    private List<Fragment> fragments;


    @Override
    public View initView() {
        View view = View.inflate(mContext, R.layout.activity_fans, null);
        viewPager = view.findViewById(R.id.addresslist_pager);
        radioGroup = view.findViewById(R.id.addresslist_rg);
        radioGroup.check(R.id.addresslist_rb1);

        imageView = view.findViewById(R.id.imageview);
        fl_alum = view.findViewById(R.id.fl_alum);
        iv_alum = view.findViewById(R.id.iv_alum);
        initListener();
        return view;
    }

    private void initListener() {
        imageView.setOnClickListener(this);
        fl_alum.setOnClickListener(this);
        iv_alum.setOnClickListener(this);
        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.addresslist_rb1:
                    viewPager.setCurrentItem(0);
                    break;
                case R.id.addresslist_rb2:
                    viewPager.setCurrentItem(1);
                    break;
            }
        });

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                //实现滑动页面下方按钮的联动
                radioGroup.check(radioGroup.getChildAt(i).getId());
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        fragments = new ArrayList<>();
        fragments.add(new AttentionPagerFragment());
        fragments.add(new FansPagerFragment());
        //viewpaer一次加载全部页面
        viewPager.setOffscreenPageLimit(fragments.size());

        viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_alum:
                String userId = SPUtils.getString(getActivity(), Constants.USER_MOBILE);
                if (TextUtils.isEmpty(SPUtils.getConversionUserJob(getActivity(), userId))) {
                    //  getUserInfo();
                    startActivity(new Intent(getActivity(), ReleaseActivity.class));
                    UIUtils.showToastSafe("请先完善名片信息");
                    return;
                }
                Intent intent = new Intent(getActivity(), AlumActivity.class);
                startActivity(intent);
                break;
            case R.id.imageview:
                // finish();
                break;
        }
    }

    //适配器
    class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }


        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }
}
